import { applicationType, ResultPagination } from "@/utils/commonType";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";

/** 获取应用管理列表 */
export const getAppList = async (tenantId, data?: object) => {
  const result = await http.request<ResultPagination<applicationType>>(
    "get",
    baseUrlApi("/app/list/" + tenantId),
    {
      params: data
    }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

/** 获取应用管理列表 */
export const updateAppStatus = async (appId: string, status: string) => {
  let url = "/app/archive/" + appId;
  if (status === "active") {
    url = "/app/active/" + appId;
  }
  const result = await http.request<ResultPagination<applicationType>>(
    "put",
    baseUrlApi(url)
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const addApp = async (data: applicationType) => {
  const result = await http.request<ResultPagination<applicationType>>("post", baseUrlApi("/app/add"), { data });
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}

export const updateApp = async (appId: string, data: applicationType) => {
  const result = await http.request<ResultPagination<applicationType>>("put", baseUrlApi("/app/update/" + appId), { data });
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
}